<template>
	<view class="active">
		<div v-if="show">
			二维码页面加载失败
		</div>
		<div>
			<image :src="imgs" mode="" @click="shares()" ></image>
			<p @click="Search()">点击图片到相册</p>
		</div>
		
	</view>
</template>
<script>
	export default {
		data() {
			return {
				agent_id: "",
				show: false,
				token: "",
				imgs: "",
				path:""
			}
		},
		created() {
			this.getyonghuid()
		},
		methods: {
			getcodeimg() {
				var that = this
				console.log("二维码的发送" + that.agent_id)
				wx.request({
					url: that.$url + '/api/v1.Tool/getAppletImg', //仅为示例，并非真实的接口地址
					data: {
						agent_id: that.agent_id
					},
					method: "GET",
					header: {
						token: that.token // 默认值
					},
					success(res) {
						console.log("二维码发送成功")
						console.log(res);
						// console.log(res.data.data)
						console.log(res)
						that.imgs = that.$url + res.data.data
					},
					fail() {
						that.show = true
					}
				})
			},
			getyonghuid() {
				var that = this
				wx.getStorage({
					key: 'token',
					success(res) {
						// console.log("token"+res.data)
						that.token = res.data
					}
				})
				wx.getStorage({
					key: 'agent_id',
					success(res) {
						that.agent_id = res.data
						// console.log("agent_id"+res.data)
						that.getcodeimg()
					}
				})
			},
			Search() {
				var that=this
				  wx.downloadFile({
				      url: that.imgs,
				      success: function (res) {
				        var benUrl = res.tempFilePath;
				        //图片保存到本地相册
				        wx.saveImageToPhotosAlbum({
				          filePath: benUrl,
				          //授权成功，保存图片
				          success: function (data) {
				            wx.showToast({
				              title: '保存成功',
				              icon: 'success',
				              duration: 2000
				            })
				          },
				          //授权失败
				          fail: function (err) {
				            if (err.errMsg) {//重新授权弹框确认
				                wx.showModal({
				                  title: '提示',
				                  content: '您好,请先授权，在保存此图片。',
				                  showCancel: false,
				                  success(res) {
				                    if (res.confirm) {//重新授权弹框用户点击了确定
				                      wx.openSetting({//进入小程序授权设置页面
				                        success(settingdata) {
				                          console.log(settingdata)
				                          if (settingdata.authSetting['scope.writePhotosAlbum']) {//用户打开了保存图片授权开关
				                            wx.saveImageToPhotosAlbum({
				                              filePath: benUrl,
				                              success: function (data) {
				                                wx.showToast({
				                                  title: '保存成功',
				                                  icon: 'success',
				                                  duration: 2000
				                                })
				                              },
				                            })
				                          } else {//用户未打开保存图片到相册的授权开关
				                            wx.showModal({
				                              title: '温馨提示',
				                              content: '授权失败，请稍后重新获取',
				                              showCancel: false,
				                            })
				                          }
				                        }
				                      })
				                    } 
				                  }
				                })
				            }
				          }
				        })
				      }
				    })
			},
			shares(){
				var that=this
				wx.previewImage({
				  current: that.imgs, // 当前显示图片的http链接
				  urls: [that.imgs] // 需要预览的图片http链接列表
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	image {
		height: 500rpx;
	}
	p{
		color: $u-type-success;
		text-align: center;
	}
</style>
